﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace ECCIT.IO
{
    public enum PathType
    {
        NotSet,
        VirtualPath,
        PhysicalPath,
    }

    public class DataItemPath
    {
        public const char C_CHAR_VirtualPathSeparator = '/';
        public static readonly string C_STR_VirtualPathSeparator = C_CHAR_VirtualPathSeparator.ToString();
        //physical directory separator
        private static readonly string m_sDirectorySeparatorString = Path.DirectorySeparatorChar.ToString();

        public PathType PathType
        {
            get { return _PathType; }

            //no set{} - set from constructor only
        }
        private PathType _PathType = PathType.NotSet;

        /// <summary>
        /// Gets the physical file path. 
        /// If a ui path was used in the constructor, then any non-file
        /// item is removed from the end. Page references are also removed.
        /// </summary>
        public string PhysicalPath
        {
            get
            {
                if (_PathType == PathType.PhysicalPath)
                    //if constructed from physical path then return that
                    return _OriginalPath;
                else if (_PathType == PathType.VirtualPath)
                    //convert from Ui path
                    return GetPhysicalFilePathFromVirtualPath(_OriginalPath);
                else
                    return "";
            }

            //no set{} - set from constructor only
        }

        /// <summary>
        /// Gets the original ui path (if object was originally constructed from a ui path).
        /// Otherwise attempts to construct a ui path from the physical path. Returns "" if this is not possible.
        /// </summary>
        public string VirtualPath
        {
            get
            {
                if (_PathType == PathType.VirtualPath)
                    //if constructed from ui path then return that
                    return _OriginalPath;
                else if (_PathType == PathType.PhysicalPath)
                    //convert from Physical path
                    return "";
                else
                    return "";
            }

            //no set{} - set from constructor only
        }

        /// <summary>
        /// Gets the original path string used in the constructor.
        /// </summary>
        public string OriginalPath
        {
            get { return _OriginalPath; }
            //no set{} - set from constructor only
        }
        private string _OriginalPath = "";

        /// <summary>
        /// Constructor. Item type is extracted from the path. 
        /// NOTE: This only applies to File-sourced items.
        /// </summary>
        /// <param name="itemPath">UI or physical path to data item</param>
        /// <param name="pathType">Type of the path, ie UI or Physical</param>
        public DataItemPath(string path, PathType pathType)
        {
            this._OriginalPath = path;
            this._PathType = pathType;
        }

        public static string GetPhysicalFilePathFromVirtualPath(string virtualPath)
        {
            string sPhysicalPath = null;

            if (virtualPath == null || virtualPath.Length == 0)
                sPhysicalPath = HttpContext.Current.Server.MapPath("/");
            else
                sPhysicalPath = HttpContext.Current.Server.MapPath(virtualPath);

            return sPhysicalPath;
        }
    }
}
